//
//  HCFileOperation.m
//  lbeaconDemo
//
//  Created by liyun on 14-7-02.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "HCFileOperation.h"
#import "HCMacroDefinition.h"

static HCFileOperation *instance = nil;

@implementation HCFileOperation

+ (HCFileOperation *)shareFileOperation
{
    if(!instance)
    {
        instance = [[HCFileOperation alloc] init];
    }
    return instance;
}

- (NSString *)returnPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    return cachesDir;
}


- (void)saveSearchHistory:(NSDictionary *)dic
                     type:(int)type
{
    NSString *documentsDirectory = [self returnPath];
    if (!documentsDirectory)
    {
        NSLog(@"Documents directory not found!");
        return ;
    }
    
    NSMutableArray *historyArr = [[NSMutableArray alloc] init];
    NSString *fileName = @"";
    switch (type)
    {
        case 1:
            fileName = @"SEARCH_HISTORY1.plist";
            break;
        case 2:
            fileName = @"SEARCH_HISTORY2.plist";
            break;
        case 3:
            fileName = @"SEARCH_HISTORY3.plist";
            break;
        case 4:
            fileName = @"SEARCH_HISTORY4.plist";
            break;
        default:
            break;
    }
    
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    if(![[NSFileManager defaultManager] fileExistsAtPath:appFile])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *documentsDirectory = [self returnPath];
        //更改到待操作的目录下
        [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
        //创建文件fileName文件名称，contents文件的内容，如果开始没有内容可以设置为nil，attributes文件的属性，初始为nil
        [fileManager createFileAtPath:fileName contents:nil attributes:nil];
        
        [historyArr addObject:dic];
    }
    else
    {
        historyArr = [NSMutableArray arrayWithArray:[self getSearchHistory:type]];
        if([historyArr count] >= HISTORYCOUNT)
        {
            [historyArr removeLastObject];
        }
//        [historyArr addObject:dic];
        [historyArr insertObject:dic atIndex:0];
    }
    NSLog(@"saveto_-->%@",appFile);
    [historyArr writeToFile:appFile atomically:YES];
}

- (NSMutableArray *)getSearchHistory:(int)type
{
    NSString *documentsDirectory = [self returnPath];
    
    NSString *fileName = @"";
    switch (type)
    {
        case 1:
            fileName = @"SEARCH_HISTORY1.plist";
            break;
        case 2:
            fileName = @"SEARCH_HISTORY2.plist";
            break;
        case 3:
            fileName = @"SEARCH_HISTORY3.plist";
            break;
        case 4:
            fileName = @"SEARCH_HISTORY4.plist";
            break;
        default:
            break;
    }
    
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSMutableArray *searchHistoryArr = [[NSMutableArray alloc] init];
    if(![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
    {
        searchHistoryArr = [NSMutableArray arrayWithObjects: nil];
    }
    else
    {
       searchHistoryArr  = [NSMutableArray arrayWithContentsOfFile:documentsDirectory];
    }
    NSLog(@"get from-->%@",documentsDirectory);
    return searchHistoryArr;
}

/************************************************************************************************
 函数描述 : 删除缓存在本地的数据
 ************************************************************************************************/
- (void)deleteCache
{
    //创建文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //获取路径
    //参数NSDocumentDirectory要获取那种路径
    NSString *documentsDirectory = [self returnPath];
    
    //更改到待操作的目录下
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject]))
    {
        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
    }
}

@end

